# -*- coding: utf-8 -*-
# Description:
# Created: tangyu 2023/5/4
import json
from flask import request
from flask_restful import Resource

from app.main.service.performance_prediction_model import PerformancePredirtionModel
from app.util.result import Result
from datetime import datetime

# 接口入口类，获取请求参数，返回响应，新加接口入口类添加一个新的class和post方法
class PerformancePredirtionAPI(Resource):

    @staticmethod
    def post():
        request_data = json.loads(request.get_data().decode("utf-8"))
        print(request_data.get('time_type',None))
        if not isinstance(request_data.get('time_type',None),str):
            return Result.error_response("time_type输入错误")
        if not isinstance(request_data.get('Y', None), str):
            return Result.error_response("Y输入错误")
        if not isinstance(request_data.get('tablename',None),str):
            return Result.error_response("tablename输入错误")
        if not isinstance(request_data.get('group_var',None),str):
            return Result.error_response("group_var输入错误")
        if not isinstance(request_data.get('algorithm',None),str):
            return Result.error_response("algorithm输入错误")
        if not isinstance(request_data.get('modelname',None),str):
            return Result.error_response("modelname输入错误")
        if not isinstance(request_data.get('upperError',None),str):
            return Result.error_response("upperError输入错误")
        if not isinstance(request_data.get('lowerError',None),str):
            return Result.error_response("lowerError输入错误")
        if not isinstance(request_data.get('charNames',None),str):
            return Result.error_response("charNames输入错误")
        try:
            datetime.strptime(request_data.get('begin_data',None),'%Y-%m-%d %H:%M:%S')
        except:
            return Result.error_response(msg="begin_data输入错误")
        try:
            datetime.strptime(request_data.get('end_data',None),'%Y-%m-%d %H:%M:%S')
        except:
            return Result.error_response(msg="end_data输入错误")
        result=PerformancePredirtionModel().prediction_model(request_data)

        # 最后返回响应将对应的结果传到下面这个方法中即可，支持base_entity及子类、base_entity及子类的列表、字典、df这几种类型，会自动转json返回
        return Result.success_response(result)
